home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt85aug.arc / MAZE.PRO < prev    next >
Text File  |  1985-08-29  |  2KB  |  56 lines

  1. /* Prolog representation of the treasure maze */
  2.  
  3. adjacent(cave_entrance,trolls).    adjacent(cave_entrance,fountain).
  4. adjacent(fountain,limbo).       adjacent(fountain,food). 
  5. adjacent(fountain,bandits).       adjacent(fountain,mermaids).
  6. adjacent(bandits,treasure).       adjacent(bandits,exit).
  7. adjacent(food,treasure).       adjacent(trolls,treasure).
  8. adjacent(mermaid,exit).        adjacent(treasure,exit).
  9.  
  10. avoid([trolls,bandits]).      /* the places to stay away from */
  11.  
  12. /*   
  13.      The following is a predicate to display a path through the 
  14.      maze avoiding the Dangers and passing through the treasure room.
  15.      To invoke it use:
  16.       
  17.            traverse(cave_entrance,exit).
  18. */
  19.  
  20. traverse(Here,There) :-
  21.      avoid(Dangers),          /* grab the list of places where not to go */
  22.      path(Here,There,Dangers,[Here]).
  23.  
  24. /* see text for explanation of the path predicate below*/
  25.  
  26. path(Here,Here,Dangers,Trail)  :-
  27.      member(treasure,Trail),   /* make sure we go by the treasure room */
  28.      reverse_write(Trail).     /* print the trail starting with Here */
  29. path(From,To,Dangers,Trail) :-
  30.      (                   /* choose an Intermediate location */
  31.       adjacent(From,Intermediate)
  32.      ;
  33.       adjacent(Intermediate,From)
  34.      ),
  35.      not member(Intermediate,Dangers),      /* is it a dangerous place? */
  36.      not  member(Intermediate,Trail),      /*  have we been  there before? */
  37.      path(Intermediate,To,Dangers,[Intermediate|Trail]).   /* extend path */
  38.  
  39. /* next, print a list in reverse order */
  40.  
  41. reverse_write([]).
  42. reverse_write([Head|Tail]) :-
  43.      reverse_write(Tail),
  44.      nl,
  45.      write(Head).
  46.  
  47. member(X,[X|_]).
  48. member(X,[_|Y]) :- 
  49.      member(X,Y).
  50. Tail]) :-
  51.      reverse_write(Tail),
  52.      nl,
  53.      write(Head).
  54.  
  55. member(X,[X|_]).
  56. member(X,[_|Y])